<?php

namespace App\Http\Controllers\Admin;

use DB;
use App\Models\Admin;
use App\Models\Permission;
use Illuminate\Http\Request;

class AdminController extends BaseController
{
    public function __construct()
    {
        $this->middleware('check.admin.permission:permission module');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $admins = Admin::all();

        return view('admins.index', [
            'admins' => $admins,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $permissions = Permission::all();

        return view('admins.create', [
            'permissions' => $permissions,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name'           => 'required|max:20|unique:admins',
            'email'          => 'required|email|max:50|unique:admins',
            'permission_ids' => 'required',
            'password'       => 'required|alpha_num|min:6|max:20',
        ], [
            'permission_ids.required' => '请至少选择一个权限',
        ]);

        DB::beginTransaction();
        try {

            $admin = Admin::create(array_merge($request->all(), [
                'password' => bcrypt($request->password)
            ]));

            // add admin-permission relationships
            $admin->permissions()->attach($request->permission_ids);

            DB::commit();
        } catch (\Exception $e) {
            DB::rollback();

            return back()->withInput()->withErrors([
                'general' => '保存失败, 服务器出错',
            ]);
        }

        return redirect('/admin/admins');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $admin = Admin::findOrFail($id);

        return view('admins.show', [
            'admin' => $admin,
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $admin = Admin::findOrFail($id);
        $permissions = Permission::all();

        return view('admins.edit', [
            'admin'       => $admin,
            'permissions' => $permissions,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $admin = Admin::findOrFail($id);

        $this->validate($request, [
            'name'           => "required|max:20|unique:admins,name,$id",
            'email'          => "required|email|max:50|unique:admins,email,$id",
            'permission_ids' => 'required',
        ], [
            'permission_ids.required' => '请至少选择一个权限',
        ]);

        DB::beginTransaction();
        try {

            $admin->update($request->all());

            // update admin-permission relationships
            $admin->permissions()->sync($request->permission_ids);

            DB::commit();
        } catch (\Exception $e) {
            DB::rollback();

            return back()->withInput()->withErrors([
                'general' => '保存失败, 服务器出错',
            ]);
        }

        return redirect('/admin/admins');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Admin::destroy($id);
    }
}
